<template>
  <div ref="container">
    <a-modal
      :title="title"
      :width="1200"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :getContainer="() => $refs.container"
      :maskStyle="{ top: '93px', left: '154px' }"
      :wrapClassName="wrapClassNameInfo()"
      :mask="isDesktop()"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="取消"
      okText="保存"
      style="top: 10%; height: 80%"
    >
     
      <a-spin :spinning="confirmLoading">
        <a-form :form="form" id="vendorModal">
          <a-row class="form-row" :gutter="24">
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
                <a-input placeholder="请输入名称" v-decorator.trim="['supplierName', validatorRules.supplierName]" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="供应商编号">
                <a-input placeholder="请输入供应商编号" v-decorator.trim="['supplierNo', validatorRules.supplierNo]" />
              </a-form-item>
            </a-col>
            <!-- </a-row>
          <a-row class="form-row" :gutter="24"> -->
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="业务员">
                <a-select
                  placeholder="选择业务员"
                  v-decorator="['salespersonId', validatorRules.salespersonId]"
                  :dropdownMatchSelectWidth="false"
                >
                  <a-select-option v-for="(item, index) in salespersonist" :key="index" :value="item.id">
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
                <a-input placeholder="请输入联系人" v-decorator.trim="['contactPerson']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
                <a-input placeholder="请输入联系电话" v-decorator.trim="['phone']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开户行">
                <a-input placeholder="请输入开户行" v-decorator.trim="['bankName']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="卡编号">
                <a-input placeholder="请输入卡编号" v-decorator.trim="['bankAccount']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="qq/微信">
                <a-input placeholder="请输入qq/微信" v-decorator.trim="['qqWechat']" />
              </a-form-item>
            </a-col>
            <!-- <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="应付余额">
                <a-input placeholder="请输入应付余额" v-decorator.trim="['payableBalance']" />
              </a-form-item>
            </a-col> -->

            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="纳税人识别号">
                <a-input placeholder="请输入纳税人识别号" v-decorator.trim="['taxIdentificationNumber']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="税率(%)">
                <a-input-number style="width: 100%" placeholder="请输入税率" v-decorator.trim="['vatRate']" />
              </a-form-item>
            </a-col>

            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
                <a-input placeholder="请输入地址" v-decorator.trim="['contactAddress']" />
              </a-form-item>
            </a-col>
            <a-col :span="24 / 2">
              <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
                <a-textarea :rows="2" placeholder="请输入备注" v-decorator.trim="['remark']" />
              </a-form-item>
            </a-col>
              <a-col :span="24 / 2">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否物流公司">
                  <a-switch default-checked @change="onChange" />
                </a-form-item>
              </a-col>
              <a-col :span="24 / 2">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否启用">
                  <a-switch default-checked @change="onChangeS" />
                </a-form-item>
              </a-col>
          </a-row>
        </a-form>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import pick from 'lodash.pick'
import { addSupplier, editSupplier, checkSupplier, userAll } from '@/api/api'
import { getAction } from '@/api/manage'
import { autoJumpNextInput } from '@/utils/util'
import { mixinDevice } from '@/utils/mixin'
export default {
  name: 'VendorModal',
  mixins: [mixinDevice],
  data() {
    return {
      status: true,
      isLo: true,
      salespersonist: '',
      title: '操作',
      visible: false,
      model: {},
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        supplierName: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 60, message: '长度在 2 到 60 个字符', trigger: 'blur' },
          ],
        },
        supplierNo: {
          rules: [
            { required: true, message: '请输入编号!' },

          ],
        },
      },
    }
  },
  mounted() {
    userAll().then((res) => {
      this.salespersonist = res.data
    })
  },
  methods: {
    onChangeS() {
      this.status = !this.status
    },
    onChange() {
      this.isLo = !this.isLo
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
'supplierCategoryId',
'supplierCategoryName',
'supplierNo',
'supplierName',
'salespersonId',
'salespersonName',
'contactPerson',
'vatRate',
'bankAccount',
'bankName',
'taxIdentificationNumber',
'phone',
'qqWechat',
'contactAddress',
'remark',
'payableBalance',
'status',
'isLogisticsCompany',
          )
        )
        autoJumpNextInput('vendorModal')
      })
    },
    close() { 
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          formData.type = '供应商'
          formData.isLogisticsCompany = this.isLo ? 1 : 0
          formData.status = this.status ? 1 : 0
          let obj
          if (!this.model.id) {
            obj = addSupplier(formData)
          } else {
            obj = editSupplier(formData)
          }
          obj
            .then((res) => {
              if (res.code === 200) {
                that.$emit('ok')
                that.$message.success(res.msg)
              that.close()
              } else {
                that.$message.warning(res.msg)
              }
                               that.confirmLoading = false

            })
            
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      // let params = {
      //   name: value,
      //   type: '供应商',
      //   id: this.model.id?this.model.id:0
      // };
      // checkSupplier(params).then((res)=>{
      //   if(res && res.code===200) {
      //     if(!res.data.status){
      //       callback();
      //     } else {
      //       callback("名称已经存在");
      //     }
      //   } else {
      //     callback(res.data);
      //   }
      // });
      callback()
    },
  },
}
</script>
<style scoped>
</style>